<?php
namespace app\platform\controller\admin;
use app\BaseController;
use app\common\model\TemplateModel; //模版
use app\common\model\AppModel;
class  Template extends  BaseController{

    protected $whiteList=[
        'mall',
        'motherhood',
        'beautiful',
        'yoga',
        'pets',
        'housekeeping',
        'daojia',
        'photography',
        'spa',
        'pifa',
    ];
    
    public function index()
    {
        $keyword = $this->request->param('keyword');
        $appId   = $this->request->param('appid/d');
        $where = [];
        if(!empty($keyword)){
            $where[] = ['name','LIKE','%'.$keyword.'%'];
        }
        if(!empty($appId)){
            $where[] = ['app_id','=',$appId];
        }
        $count = TemplateModel::where($where)->count();
        $datas = TemplateModel::with(['app'])->where($where)
                 ->order(['template_id'=>'desc'])
                ->page($this->request->page)
                ->limit($this->request->limit)    
                ->select()->toArray();
        $apps = AppModel::where([
//            ['type','<>',1],
            ['iden','in',$this->whiteList],
            ['is_delete','=',0]
        ])->select()->toArray();
        return $this->success([
            'total' => $count,
            'datas' => $datas,
            'apps'  => $apps
        ]);
    }
    
    public function delete(){
         $template_id = $this->request->param('templateId/d');
        if(empty($template_id)){
            return $this->error('请选择要查看的模版');
        }
        if(null === $template = TemplateModel::find($template_id)){
            return $this->error('请选择要查看的模版');
        }
        $template->delete();
        return $this->success('删除成功');
    }
    
    public function show(){
        $template_id = $this->request->param('templateId/d');
        if(empty($template_id)){
            return $this->error('请选择要查看的模版');
        }
        if(null === $template = TemplateModel::find($template_id)){
            return $this->error('请选择要查看的模版');
        }
        return $this->success([
            'template_id' => $template->template_id,
            'app_id'      => $template->app_id,  
            'name'        => $template->name,
            'photo'       => $template->photo,
            'style'       => $template->style,
        ]);
    }
    
    public function online(){
        $template_id = $this->request->param('templateId/d');
        if(empty($template_id)){
            return $this->error('请选择要操作的模版');
        }
        if(null === $template = TemplateModel::find($template_id)){
            return $this->error('请选择要操作的模版');
        }
        $template->is_online = $template->is_online == 1 ? 0 : 1;
        $template->save();
        return $this->success('操作成功');
    }
    
    public function save(){
        $appId = $this->request->param('appId/d');
        $templateId = $this->request->param('templateId/d');
        if(empty($appId)){
            return $this->error('请选择模版对应的应用');
        }
        if(null === $app = AppModel::find($appId)){
            return $this->error('模版对应的应用不存在');
        }
        $style = $this->request->param('style','', 'security_json_or_array');
        if(empty($style)){
            return $this->error('请提交风格');
        }
        $name = $this->request->param('name');
        $photo = $this->request->param('photo');
        if(empty($name)){
            return $this->error('模版风格名称不能为空');
        }
        if(empty($photo)){
            return $this->error('模版缩略图不能为空');
        }
        if(!empty($templateId)){
            if(null === $template = TemplateModel::find($templateId)){
                return $this->error('请选择要更改的模版风格');
            }
        }else{
            $template = new TemplateModel();
        }
        $template->name = $name;
        $template->photo = $photo;
        $template->style = $style;
        $template->app_id = $appId;
        $template->iden   = $app->iden;
        $template->save();
        return $this->success('保存成功');
    }
    
}
